<?php
namespace Amoysea\Controller;
use Think\Controller;
class BaseController extends Controller {
    public function _initialize(){
        $this->rootCheck();
    }
    protected function rootCheck(){
		
        if(null==session('company')){
            $this->redirect('Reg/login');
        }
		$company_status=session('company.status');
		$childs=session('company.childs');
		// dump(session());
		// die;
		
		if($company_status<>1){
			$this->error('账号正在审核中，请等待通过后再登录。。。');
		}
		// dump(!$company_status);
		// dump(empty($company_cilids_status));
		// die;
		if(!empty($childs)){
			$company_cilids_status=$childs['status'];
			if($company_cilids_status<>1){
				$this->error('账号正在审核中，请等待通过后再登录。。。');
			}
			$this->assign('childs_company',$childs);
			$g=session('company.childs');
		}else{
			$g=session('company');
		}
		// dump($childs);
		// die;
		
        
		C('erp_dbname',$g['db']);
		$base_config=M('Config', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));//加载配置信息
		$list=$base_config->getField('en_name,value');
		C('base_config',$list);
        //////权限检查
        if($g['type']==0) {
            if(session('user_root')!=null){
                $root=session('user_root');
            }else{
                $department_id = $g['department_id'];
                $db_name = $g['db'];
                $m = M('department', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
                $root_group_id = $m->where(array('id' => $department_id))->getField('root_group_id');
                $m = M('root', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
                $group_id = explode(',', $root_group_id);
                $where['id'] = array('in', $group_id);
                $all_root = $m->where($where)->field('controller,action')->select();
                $root = array();
                foreach ($all_root as $v) {
                    if (strpos($v['action'], ',') === false) {
                        $root[$v['controller']][] = $v['action'];
                    } else {
                        $new_root = explode(',', $v['action']);
                        foreach ($new_root as $u) {
                            $root[$v['controller']][] = $u;
                        }
                    }
                }
                unset($all_root);
                session('user_root',$root);
                ///////开始权限判断
            }
			
            //echo "<pre>";
			// dump(session());
            // var_dump($root);
            $is_root = 0;
            foreach ($root as $k => $v) {
                if ($k == CONTROLLER_NAME && in_array(ACTION_NAME, $v)) {
                    $is_root = 1;
                }
            }
            if ($is_root==0) {
                $no_arr = array(//额外的都具备的权限
                    'Index' => array('index', 'welcome'),
                    'Unit' => array('getUnit','getCoin','getCountry'),
                    'Category' => array('getCategory'),
                    'Role' => array('getRole', 'getEditRole'),
                    'List' => array('warehouse_select_session_delete', 'warehouse_select'),
                    'Product'=>array('getBook','getWarehouse'),
					'Salesman'=>array('goods'),
					'Order'=>array('tadd_goods','area_list','city_list','outfee','goods_price','edit_storagegoodsnums','verification','exportExcel'),
					'Checkbill'=>array('verification','confirm_adds'),
					'Ems'=>array('curl_web','ems_auto','ems_fee'),
                );
                foreach($no_arr as $k=>$v){
                    if($k == CONTROLLER_NAME && in_array(ACTION_NAME, $v)){
                        $is_root=1;
                    }
                }
				// dump($is_root);
				// die;
                if($is_root==0){
                    if(IS_AJAX){
                        alert(0,'没有权限');
                    }else{
                        echo '没有权限';
                        exit;
                    }
                }
            }
        }
    }
}